<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-house" style="font-weight: bold">&nbsp;&nbsp;后台管理</i>
        </el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-marketing">&nbsp;&nbsp;销售管理</i></el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-circle-plus">&nbsp;&nbsp;合同管理</i></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div>
      <div class="data-canRead" style="width: 100%;background-color: white;height: auto">
        <el-divider content-position="left">数据添加</el-divider>
        <el-form
            :model="agreementForm"
            :rules="rules"
            ref="agreementForm"
            label-width="100px"
            style="margin-right: 25px">
          <el-form-item label="商品代码:" prop="name">
            <el-input v-model="agreementForm.name" placeholder="请输入客户名称"></el-input>
          </el-form-item>
          <el-row>
            <el-col :span="8">
              <el-form-item label="销售类型:" prop="salesType">
                <el-select v-model="agreementForm.salesType" placeholder="请选择销售类型" style="width: 80%">
                  <el-option
                      v-for="item in salesTypes"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="销售日期:" prop="dateSale">
                <el-date-picker
                    v-model="agreementForm.dateSale"
                    type="date"
                    style="width: 90%"
                    placeholder="选择销售日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="单号:" prop="oddNumber">
                <el-input v-model="agreementForm.oddNumber" placeholder="输入单号"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-divider content-position="left">金额提交</el-divider>
          <el-row>
            <el-col :span="8">
              <el-form-item label="销售金额:" prop="sales">
                <el-input v-model="agreementForm.sales" placeholder="输入销售金额"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="退款金额:" prop="drawback">
                <el-input v-model="agreementForm.drawback" placeholder="输入退款金额"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="扣款:" prop="deduction">
                <el-input v-model="agreementForm.deduction" placeholder="输入扣款"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="20">
              <el-form-item label="总计金额:" prop="sum">
                <el-input v-model="agreementForm.sum" placeholder="输入总计金额"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item style="float: right">
                <el-button type="primary"@click="compute" style="background-color: white;border-color: gray">
                  <i class="el-icon-thumb" style="color:gray;font-size: 100%">&nbsp;&nbsp;计算</i>
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="回款日期:" prop="dateColl">
                <el-date-picker
                    v-model="agreementForm.dateColl"
                    type="date"
                    placeholder="选择回款日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="回款:" prop="collection">
                <el-input v-model="agreementForm.collection" placeholder="输入回款"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="应收到账期:" prop="dateReceive">
            <el-date-picker
                v-model="agreementForm.dateReceive"
                type="date"
                placeholder="选择应收到账期">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('agreementForm')" style="color: white">添加</el-button>
            <el-button type="warning" @click="resetForm('agreementForm')" style="color: white">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      agreementForm: {
        name: '达内',
        salesType: '支付宝',
        dateSale: '2021-06-01',
        oddNumber: 'ds10110',
        sales: 200000,
        drawback: 1200,
        sum: 0,
        deduction: 3700,
        collection: 23000,
        dateColl: '2021-8-15',
        dateReceive: '2021-07-17',
      },
      salesTypes: [
        {
          label: "支付宝",
          value: "支付宝"
        },
        {
          label: "微信",
          value: "微信"
        },
        {
          label: "公司账号",
          value: "公司账号"
        },
        {
          label: "转账",
          value: "转账"
        },
        {
          label: "现金",
          value: "现金"
        },
      ],
      rules: {
        name: [
          {required: true, message: "请输入客户名称", trigger: 'change'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        salesType: [
          {required: true, message: "请选择销售类型", trigger: 'change'},
        ],
        dateSale: [
          {required: true, message: "请选择销售日期", trigger: 'change'},
        ],
        oddNumber: [
          {required: true, message: "请输入单号", trigger: 'blur'},
          {pattern: /^[a-zA-Z0-9]+$/, message: "请输入英文跟数字组合", trigger: 'blur'}
        ],
        sales: [
          {required: true, message: "请输入销售金额", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        drawback: [
          {required: true, message: "请输入退款金额", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        sum: [
          {required: true, message: "请输入总计金额", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        deduction: [
          {required: true, message: "请输入扣款", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        collection: [
          {required: true, message: "请输入回款", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        dateColl: [
          {required: true, message: "请选择回款日期", trigger: 'change'},
        ],
        dateReceive: [
          {required: true, message: "请选择应收到账期", trigger: 'change'},
        ],
      }
    }
  },
  methods: {
    compute(){
      let sum = parseInt(this.agreementForm.sales) - parseInt(this.agreementForm.drawback);
      this.$set(this.agreementForm,'sum',sum);
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9080/customAgreements/add-new';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.agreementForm);
          console.log('formData = ' + formData);

          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            console.log(responseBody);
            if (responseBody.state == 20000) {
              this.$message({
                showClose: true,
                message: '添加成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message({
                showClose: true,
                message: responseBody.message,
                type: 'error'
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

  },

}

</script>


<style scoped>
* {
  color: black;
}

.data-canRead {
  width: 380px;
  height: 380px;
  margin-top: 15px;
  box-shadow: 0px 5px 5px #8d858a;
  float: left;
  margin-right: 16px;
}
</style>